<?php
/**
 * controller file Created by PhpStorm.
 * User: wumengmeng
 * Date: 2019/09/03
 * Time: 17:42
 */

namespace Modules\Shop\Http\Controllers;

use HiCommon\Service\OrderCheckpointRefundService;
use HiCommon\Service\OrderService;
use HiCommon\Service\PayRefundService;
use Illuminate\Support\Facades\Request;
use Modules\Admin\Http\Controllers\BaseController;
use Modules\Admin\Service\OrderCheckpointService;

class OrderCheckpointController extends BaseController
{

    /**
     *  年检预约订单 列表 - 全部
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function index()
    {
        try {
            $n_shop_id= shop_shop_id();
            $arr_input = Request::input();
            $type = $arr_input['search_order_type'] = empty($arr_input['search_order_type']) ? 'all':str_trim($arr_input['search_order_type']);

            switch ($type)
            {
                case 'all':
                    $res = OrderCheckpointService::all_order_list_by_shopid($n_shop_id,$arr_input);

                    break;

                case 'waiting':
                    $res = OrderCheckpointService::waiting_order_list_by_shopid($n_shop_id,$arr_input);

                    break;

                case 'complete':
                    $res = OrderCheckpointService::complete_order_list_by_shopid($n_shop_id,$arr_input);

                    break;
                default:
                    $res = OrderCheckpointService::all_order_list_by_shopid($n_shop_id,$arr_input);

            }

            echo_error($res);

//            dd($res['data']);
            $res['data']['arr_tab'] = [
              'all'=>'全部',
              'waiting'=>'待处理',
              'complete'=>'已完成',
            ];

            $res['data']['arr_search_type'] = [
              'order_sn'=>'订单编号',
              'plate_no'=>'车牌号码',
            ];

            return view('shop::order_checkpoint.index', $res['data']);
        }
        catch (\Exception $exception) {
            return hello_error('失败-'.$exception->getMessage(),[],$exception->getMessage());
        }

    }

//    /**
//     *  年检预约订单 列表 - 待处理
//     *
//     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
//     * @author wumengmeng <wu_mengmeng@foxmail.com>
//     */
//    public function index_waiting()
//    {
//        try {
//            $n_shop_id= shop_shop_id();
//            $arr_input = Request::input();
//            $res = OrderCheckpointService::waiting_order_list_by_shopid($n_shop_id,$arr_input);
//            echo_error($res);
//            return view('shop::order_checkpoint.index_waiting', $res['data']);
//        }
//        catch (\Exception $exception) {
//            return hello_error('失败-'.$exception->getMessage(),[],$exception->getMessage());
//        }
//    }
//
//    /**
//     *  年检预约订单 列表 - 已完成
//     *
//     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
//     * @author wumengmeng <wu_mengmeng@foxmail.com>
//     */
//    public function index_complete()
//    {
//        try {
//            $n_shop_id= shop_shop_id();
//            $arr_input = Request::input();
//            $res = OrderCheckpointService::complete_order_list_by_shopid($n_shop_id,$arr_input);
//            echo_error($res);
//            return view('shop::order_checkpoint.index_complete', $res['data']);
//        }
//        catch (\Exception $exception) {
//            return hello_error('失败-'.$exception->getMessage(),[],$exception->getMessage());
//        }
//    }

    /**
     *  退款申请列表
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function apply_refund_waiting_list()
    {
        try {
            $arr_input = Request::input();
            $arr_input['shop_id'] = shop_shop_id();
            $type = $arr_input['search_order_type'] = empty($arr_input['search_order_type']) ? 'apply_waiting':str_trim($arr_input['search_order_type']);
            
//            dd($type);

            switch ($type)
            {
                //待处理退款申请列表
                case 'apply_waiting':
                    $res = OrderCheckpointRefundService::api_apply_refund_waiting_list($arr_input);
                    break;

                //待处理退款列表
                case 'refund_waiting':
                    $res = OrderCheckpointRefundService::api_refund_waiting_list($arr_input);
                    break;

                //已拒绝申请列表
                case 'apply_refused':
                    $res = OrderCheckpointRefundService::api_apply_refund_refused_list($arr_input);
                    break;

                //已退款列表
                case 'refunded':
                    $res = OrderCheckpointRefundService::api_refunded_list($arr_input);
                    break;

                default:
                    $res = OrderCheckpointRefundService::api_apply_refund_waiting_list($arr_input);
            }

            echo_error($res);

//                        dd($res['data']);
            $res['data']['arr_tab'] = [
              'apply_waiting'=>'退款申请列表',
              'refund_waiting'=>'待退款列表',
              'apply_refused'=>'已拒绝申请列表',
              'refunded'=>'已退款列表',
            ];

            $res['data']['arr_search_type'] = [
//              'order_sn'=>'订单编号',
              'refund_sn'=>'售后编号',
              'plate_no'=>'车牌号码', // TODO 老是不成功
            ];


            return view('shop::order_checkpoint.apply_refund_waiting_list', $res['data']);

        }
        catch (\Exception $exception) {
            echo_error(hello_error('失败-'.$exception->getMessage(),[],$exception->getMessage()));
        }

    }

    /**
     * 待退款列表
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function refund_waiting_list()
    {
        try {
            $arr_input = Request::input();
            $arr_input['shop_id'] = shop_shop_id();
            $res = OrderCheckpointRefundService::api_refund_waiting_list($arr_input);
            echo_error($res);
            return view('shop::order_checkpoint.refund_waiting_list', $res['data']);

        }
        catch (\Exception $exception) {
            echo_error(hello_error('失败',[],$exception->getMessage()));
        }

    }

    public function apply_refund_agree_page()
    {
        try {
            $n_id = intval(Request::input('id',0));
            if($n_id <= 0){
                msg_error('数据id不能为空');
            }
            return view('shop::order_checkpoint.apply_refund_agree_page', ['n_id'=>$n_id]);
        }
        catch (\Exception $exception) {
            return hello_error('失败-'.$exception->getMessage(),[],$exception->getMessage());
        }

    }

    /**
     * 同意退款申请-等待退款
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function apply_refund_agree(){
        try {
            $n_id = Request::input('id',0);
            shiwu_start();
            $res = OrderCheckpointRefundService::api_apply_refund_agree($n_id);
            shiwu_end($res);
            return $res;
        }
        catch (\Exception $exception) {
            return hello_error('失败-'.$exception->getMessage(),[],$exception->getMessage());
        }

    }

    /**
     * 拒绝退款申请-页面
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function apply_refund_refuse_page(){
        try {
            $n_id = intval(Request::input('id',0));
            if($n_id <= 0){
                msg_error('数据id不能为空');
            }
            return view('shop::order_checkpoint.apply_refund_refuse_page', ['n_id'=>$n_id]);
        }
        catch (\Exception $exception) {
            return hello_error('失败-'.$exception->getMessage(),[],$exception->getMessage());
        }
    }

    /**
     * 拒绝退款申请
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function apply_refund_refuse(){
        try {
            $n_id = Request::input('id',0);
            $s_refuse_reason = Request::input('refuse_reason',0);

            shiwu_start();
            $res = OrderCheckpointRefundService::api_apply_refund_refuse($n_id,$s_refuse_reason);
            shiwu_end($res);
            return $res;
        }
        catch (\Exception $exception) {
            return hello_error('失败-'.$exception->getMessage(),[],$exception->getMessage());
        }
    }



    /**
     * 退款处理
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function deal_refund(){
        try {
            $s_order_sn = str_trim(Request::input('order_sn',''));
            $s_refund_sn = str_trim(Request::input('refund_sn',''));

            shiwu_start();
            $res = PayRefundService::api_online_pay_refund($s_order_sn,$s_refund_sn);
            shiwu_end($res);

            return $res;
        }
        catch (\Exception $exception) {
            return hello_error('失败-'.$exception->getMessage(),[],$exception->getMessage());
        }
    }

    /**
     * 年检预约订单 - 详情
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function detail()
    {
        try {
            $n_id = intval(Request::input('id',0));
            $res = OrderService::get_order_checkpoint_by_id($n_id);
//            dd($res);
            echo_error($res);
            return view('shop::order_checkpoint.detail',['arr_data'=> $res['data']]);

        }
        catch (\Exception $exception) {
            echo_error(hello_error('失败',[],$exception->getMessage()));
        }
    }

    /**
     * 年检预约订单 - 检测完成(全部车)
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function complete_all()
    {
        try {
            $n_id = intval(Request::input('id',0));
            $n_shop_id = intval(shop_shop_id());
            shiwu_start();
            $res = \HiCommon\Service\OrderCheckpointService::api_deal_complete_all($n_id,$n_shop_id);
            shiwu_end($res);
            return  $res;
        }
        catch (\Exception $exception) {
            return hello_error('失败',[],$exception->getMessage());
        }
    }

    /**
     *  年检预约订单 - 检测失败(全部车-退款)-页面
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function fail_all_page()
    {
        try {
            $n_id = intval(Request::input('id',0));
            if($n_id <= 0){
                msg_error('数据id不能为空');
            }
            return view('shop::order_checkpoint.fail_all_page', ['n_id'=>$n_id]);
        }
        catch (\Exception $exception) {
            return hello_error('失败-'.$exception->getMessage(),[],$exception->getMessage());
        }
    }

    /**
     * 年检预约订单 - 检测失败(全部车-退款)-操作
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function fail_all()
    {
        try {
            $n_id = intval(Request::input('id',0));
            $s_remark = trim(Request::input('remarks',''));
            $n_shop_id = intval(shop_shop_id());
            shiwu_start();
            $res = \HiCommon\Service\OrderCheckpointService::api_deal_fail_all($n_id,$n_shop_id,$s_remark);
            shiwu_end($res);
            return  $res;
        }
        catch (\Exception $exception) {
            return hello_error('失败',[],$exception->getMessage());
        }

    }


    /**
     * 年检预约订单 - 检测合格
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function check_ok()
    {
        try {
            $n_id = intval(Request::input('id',0));
            $n_shop_id = intval(shop_shop_id());
            shiwu_start();
            $res = \HiCommon\Service\OrderCheckpointService::api_deal_complete_single($n_id,$n_shop_id);
            shiwu_end($res);
            return  $res;
        }
        catch (\Exception $exception) {
            return hello_error('失败',[],$exception->getMessage());
        }
    }

    /**
     * 年检预约订单 - 检测不合格
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function check_not_ok()
    {
        try {
            $n_id = intval(Request::input('id',0));
            $n_shop_id = intval(shop_shop_id());
            shiwu_start();
            $res = \HiCommon\Service\OrderCheckpointService::api_single_check_not_ok($n_id,$n_shop_id);
            shiwu_end($res);
            return  $res;
        }
        catch (\Exception $exception) {
            return hello_error('失败',[],$exception->getMessage());
        }
    }

    /**
     *  年检预约订单 故障- 检测失败(单辆车-退款)-页面
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function fail_single_page()
    {
        try {
            $n_id = intval(Request::input('id',0));
            if($n_id <= 0){
                msg_error('数据id不能为空');
            }
            return view('shop::order_checkpoint.fail_single_page', ['n_id'=>$n_id]);
        }
        catch (\Exception $exception) {
            return hello_error('失败-'.$exception->getMessage(),[],$exception->getMessage());
        }
    }

    /**
     *  年检预约订单 故障- 检测失败(单辆车-退款)-操作
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function fail_single()
    {
        try {
            $n_id = intval(Request::input('id',0));
            $s_remark = trim(Request::input('remarks',''));
            $n_shop_id = intval(shop_shop_id());
            shiwu_start();
            $res = \HiCommon\Service\OrderCheckpointService::api_deal_fail_single($n_id,$n_shop_id,$s_remark);
            shiwu_end($res);
            return  $res;
        }
        catch (\Exception $exception) {
            return hello_error('失败',[],$exception->getMessage());
        }
    }








}